package com.ruoyi.api.controller;

import cn.hutool.db.sql.Direction;
import com.ruoyi.api.service.FenceService;
import com.ruoyi.api.vo.FenceInfo;
import com.ruoyi.api.vo.FenceIpage;
import com.ruoyi.common.core.domain.AjaxResult;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 电子围栏
 */
@RestController
@RequestMapping("/api/sys/fence")
public class FenceController {

    @Autowired
    private FenceService fenceService;

    /**
     * 新增电子围栏
     * @param fenceInfo
     * @return
     */
    @PostMapping("/addFence")
    public AjaxResult addFence(@RequestBody FenceInfo fenceInfo){
        fenceService.add(fenceInfo);
        return AjaxResult.success();
    }

    /**
     * 删除电子围栏
     * @param fenceId
     * @return
     */
    @DeleteMapping("deleteFence")
    public AjaxResult delectFence(@RequestParam Integer fenceId){
        boolean a = fenceService.delect(fenceId);
        if(a){
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 按条件查询电子围栏
     * @param
     * @return
     */
    @GetMapping("selectFence")
    public AjaxResult selectFence(@RequestParam Integer pageNumber,
                                  @RequestParam Integer pageLimit){
        FenceIpage fence = new FenceIpage();
        List<FenceInfo> list = fenceService.selectAll();
        if(list.isEmpty()){
            return AjaxResult.error("没有围栏数据,请添加");
        }
        Date date = new Date();
        DateFormat dateFormats = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = dateFormats.format(date);
        for (int i = 0; i < list.size()-1; i++) {
            if(list.get(i).getEndForceTime() == null){
                break;
            }
            //判断是否超过过期时间,超过过期时间的删除
            if(times.compareTo(list.get(i).getEndForceTime()) > 0) {
                fenceService.delect(list.get(i).getFenceId());
                list.remove(i);
            }
        }
        //分页查询
        List<FenceInfo> Ipage = fenceService.selectPage(pageNumber,pageLimit);
        int count = list.size();
        fence.setInfoList(Ipage);
        fence.setCount(count);
        fence.setPageLimit(pageLimit);
        fence.setPageNumber(pageNumber);
        return AjaxResult.success(fence);
    }

    /**
     * 查询所有的电子围栏
     * @return
     */
    @PostMapping("selectAllFence")
    public AjaxResult selectAllFence(){
        Date date = new Date();
        DateFormat dateFormats = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = dateFormats.format(date);
        List<FenceInfo> list = fenceService.selectAll();
        for (FenceInfo fenceInfo : list) {
            //判断是否超过过期时间,超过过期时间的删除
            if(times.compareTo(fenceInfo.getEndForceTime()) > 0){
                fenceService.delect(fenceInfo.getFenceId());
                list.remove(fenceInfo);
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 修改电子围栏
     * @param fenceInfo
     * @return
     */
    @PutMapping("updateFence")
    public AjaxResult updateFence(@RequestBody FenceInfo fenceInfo){
        boolean a = fenceService.update(fenceInfo);
        if(a){
            return AjaxResult.success("修改成功！");
        }
        return AjaxResult.error("修改失败！");
    }

}
